"use strict";

const {
  app: electronApp,
} = require("electron");
const _ = require("lodash");
const { JsondbService } = require('../service/database/jsondb');
/**
 * 登录窗口功能控制器
 * @class
 */
class settingController {

  async saveSetting(args) {
    // 保存设置信息到本地配置文件
    const { subject,check_weekly,gender,nickname,startup } = args;
    const data = {
        result: null,
        code: 200,
      };
    data.result = await JsondbService.addTestData(args);
    return data;    
  }

  async jsondbOperation(args) {
    const { action, info, search_age, data_dir } = args;

    const data = {
      action,
      result: null,
      all_list: [],
    };
    switch (action) {
      case "add":
        data.result = await JsondbService.addTestData(info);
        break;
      case "del":
        data.result = await JsondbService.delTestData(
          info.key
        );
        break;
      case "updateSetting":
        data.result = await JsondbService.updateSetting(info);
        break;
      case "delSetting":
        data.result = await JsondbService.delSetting(
          info.key
        );
        break;

      case "readSetting":
        data.result = await JsondbService.readSetting(
          info.key
        );
        break;
      case "update":
        data.result = await JsondbService.updateBatchData(
          info.name,
          info.age
        );
        break;
      case "get":
        data.result =
          await JsondbService.getTestData(search_age);
        break;
      case "getDataDir":
        data.result = await JsondbService.getDataDir();
        break;
      case "setDataDir":
        data.result =
          await JsondbService.setCustomDataDir(data_dir);
        break;
    }

    data.all_list = await JsondbService.getAllTestData();

    return data;
  }


  //初始化应用程序设置
  async getSettings() {
    const data = {
      result: null,
    };
    data.result = await JsondbService.getDataDir();
    return data;
  }

 async getMetaData() {
    let args= ["subject","grade","question_type"];
    const data = { 
      result: null,     
      code: 400,
    };
    let promises = args.map(async (arg) => {
        let res = await Services.get("database.metadb").getData(arg);
        return { [arg]: res };
    });
    data.result = await Promise.all(promises);
    return data;
 }

}

settingController.toString = () => "[class settingController]";
module.exports = settingController;
